const {
  $util,
} = getApp();
Page({
  data: {
    money: 0,
    remark: '备注信息',
    showPropup: false,
    info: '',
    userInfo: null,
    moneyList: [],
    cateid: '',
    fee: '', //
    content: '', // 线下缴费备注
    showSalary: false,
    nowDate: getApp().util.formateDateTime(new Date().getTime() / 1000),
    imgUrl: $util.getImgUrl(),
    isLoad: false,
  },
  onLoad(ops) {
    wx.setNavigationBarTitle({
      title: '党费缴纳',
    });
  },
  onShow() {
    let wxData = wx.getStorageSync('qyUserInfo') || {};
    this.setData(wxData);
    this.setData({
      isSaaS: wx.getStorageSync('isSaaS'),
    });
    this.loadDetail();
    this.loadOrderList();
  },
  // 缴费订单列表
  loadOrderList() {
    $util.request({
      url: 'entry/wxapp/partyman.dues.orderlist',
      data: {
        page: 1,
      },
      success: (res) => {
        this.setData({
          cList: res.data || [],
          cTime: res.data && res.data[0] && res.data[0].ctime.split(' ')[0] || '',
          cMoney: res.data && res.data[0] && res.data[0].fee || 0,
        });
      },
    });
  },
  loadDetail() {
    $util.request({
      url: 'entry/wxapp/partyman.dues.index',
      data: {},
      success: (res) => {
        console.log(res)
        this.loadPayDetail();
        this.setData({
          info: res.content,
          money: res.fee,
          remark: res.remark,
          userInfo: res.info,
          showIntro: Number(res.content) === 1,
          cateid: Number(res.type),
          img: res.img || [],
          is_salary: res.is_salary,
          pic: res.paybannerurl,
          isLoad: true,
          ispay: res.ispay,
        });
      },
    });
  },
  loadPayDetail(e) {
    $util.request({
      url: 'entry/wxapp/partyman.dues.getrecord',
      data: {},
      success: (res) => {
        this.setData({
          moneyList: res.data,
          moneyIntro: res.totalstr,
        });
      },
    });
  },
  bindInputValue(e) {
    const {
      id,
    } = e.currentTarget.dataset;
    let data = {};
    data[id] = e.detail.value;
    this.setData(data);
  },
  payment() {
    let that = this;
    if (Number(this.data.money) === 0) {
      wx.showToast({
        title: '当前没有需要缴纳的党费',
        icon: 'none',
      });
      return;
    }

    if (that.data.isSaaS) {
      const url = `/ws_party_const/pages/payment/pay`;
      wx.navigateTo({
        url,
      });
    } else {
      $util.request({
        url: 'entry/wxapp/partyman.dues.partypay',
        data: {},
        success: (res) => {
          const {
            nonceStr,
            paySign,
            signType,
            timeStamp,
          } = res.payinfo;
          wx.requestPayment({
            timeStamp,
            nonceStr,
            package: res.payinfo.package,
            paySign,
            signType,
            success: () => {
              wx.showToast({
                title: '支付成功',
                duration: 1e3,
              });
              setTimeout(() => {
                this.loadOrderList();
                this.loadDetail();
                this.hidePropup();
              }, 1e3);
            },
          });
        },
      });
    }
    
  },
  submit() {
    if (!this.data.content) {
      wx.showToast({
        title: '请填写备注',
        icon: 'none',
      });
      return;
    }
    wx.showModal({
      title: '提示',
      content: '确定要提交吗？',
      success: res => {
        if (res.confirm) {
          $util.request({
            url: 'entry/wxapp/partyman.dues.manualPartyPay',
            data: {
              fee: this.data.fee,
              content: this.data.content,
            },
            success: (res) => {
              wx.showToast({
                title: res.message,
                duration: 1e3,
                mark: true,
                success: () => {
                  this.setData({
                    fee: '',
                    content: '',
                  });
                },
              });
            },
          });
        }
      },
    });
  },
  /**
   * 显示弹出框
   */
  showPropup() {
    this.setData({
      showPropup: true,
      propup: {
        mask: true,
        confirmText: '确认',
        cancelText: '取消',
        title: '缴费详情',
        showCancel: false,
      },
    });
  },
  /**
   * 隐藏弹出框
   */
  hidePropup() {
    this.setData({
      showPropup: false,
      showSalary: false,
    });
  },
  /**
   * 点击确认按钮
   */
  confirm() {
    this.hidePropup();
  },
  /**
   * 点击取消按钮
   */
  cancel() {
    this.hidePropup();
  },
  /**
   * 阻止默认事件
   */
  preventDefault() {
    return false;
  },
  previewImg(e) {
    wx.previewImage({
      current: this.data.img[e.currentTarget.dataset.idx],
      urls: this.data.img,
    });
  },
  // 显示党费计算输入框
  showSalaryInput() {
    this.setData({
      showSalary: true,
      propup: {
        mask: true,
        confirmText: '确认',
        cancelText: '取消',
        title: '输入工资计算党费',
        showCancel: false,
      },
    });
  },
  calculateSalary() {
    let { salary } = this.data;
    if (!Number(salary) || !(/((^[1-9]\d*)|^0)(\.\d{0,2}){0,1}$/).test(salary)) {
      wx.showToast({
        title: '请填写正确的工资金额',
        icon: 'none',
      });
      return;
    }
    $util.request({
      url: 'entry/wxapp/partyman.dues.setSalary',
      data: {
        salary,
      },
      success: (res) => {
        this.setData({
          showSalary: false,
          money: res.fee,
        });
        this.loadDetail();
      },
    });
  },

  callService() {
    wx.makePhoneCall({
      phoneNumber: '18068266923',
    });
  },
});